<?php
/**
 * Created by PhpStorm.
 * User: jack Wang
 * Date: 2021/8/16
 * Time: 10:49
 */

class Users_model extends Base_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('common_helper');
    }

    //获取用户是否存在
    public function checkUser($id)
    {
        $query = $this->db->where('id', $id)->get('users');
        return $query->num_rows() ? true : false;
    }

    //获取手机号是否存在
    public function existPhone($phone)
    {
        $query = $this->db->select('id')
            ->where('phone', $phone)
            ->get('users');
        return $query->num_rows() ? true : false;
    }

    public function getUserCount()
    {
        return $this->commonQuery('count(id) count', [], 'users');
    }

    //通过用户名查找用户信息
    public function findByUsername($username)
    {
        $this->db->where('username', $username);
        $this->db->select('*');
        $query = $this->db->get('users');
        return $query->row_array();
    }

    public function EducationInfo($id)
    {
        $query = $this->db->where('user_id', $id)->select('*')->get('education');
        return $query->num_rows() ? $query->row_array() : false;
    }

    public function alertEducationInfo($data)
    {
        $res = $this->EducationInfo($data['user_id']);
        //数据库中一存在该用户的教育信息
        if ($res) {
            $this->db->update('education', $data, ['id' => $res['id']]);
        } else {
            $this->db->insert('education', $data);
        }
        return $this->db->affected_rows() ? true : [];
    }

    public function findById($id)
    {
        $query = $this->db->select()
            ->where('id', $id)
            ->get('users');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function alertUserInfo($data)
    {
        $this->db->update('users', $data, ['id' => $data['id']]);
        return $this->db->affected_rows() ? true : false;
    }

    public function accountSetting($id)
    {
        $query = $this->db->select('email,phone')
            ->where('id', $id)
            ->get('users');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function register($data)
    {
        $this->db->insert('users', $data);
        return $this->db->affected_rows() ? $this->db->insert_id() : false;
    }

    public function is_follow($data)
    {
        //判断该用户是否已经关注该主播
        $query = $this->db->select('user_id')
            ->where('user_id', $data['user_id'])
            ->where('follow_id', $data['follow_id'])
            ->get('follow');
        //判断该用户
        return $query->num_rows() ? true : false;
    }

    public function follow($data)
    {
        //这里需要开启事务操作，首先将数据插入follow表中，然后更新用户的粉丝数量有一步出错就回滚事务
        //开启事务
        $this->db->trans_begin();
        //更新用户表中的用户粉丝数
        $sql1 = "update users set fans = fans+1 where id = " . $data['follow_id'];
        $this->db->query($sql1);
        //跟新用户表中的用户关注数
        $sql2 = "update users set follow = follow+1 where id = " . $data['user_id'];
        $this->db->query($sql2);
        //数据插入follow表中
        $data['create_time'] = time();
        $this->db->insert('follow', $data);

        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
        return $this->db->trans_status() ? true : false;
    }

    public function unFollow($data)
    {
        //这里需要开启事务操作，首先将数据插入follow表中，然后更新用户的粉丝数量有一步出错就回滚事务
        //开启事务
        $this->db->trans_begin();
        //更新用户表中的用户粉丝数
        $sql1 = "update users set fans = fans-1 where id = " . $data['follow_id'];
        $this->db->query($sql1);
        //跟新用户表中的用户关注数
        $sql2 = "update users set follow = follow-1 where id = " . $data['user_id'];
        $this->db->query($sql2);
        //数据插入follow表中
        $this->db->delete('follow', ['user_id' => $data['user_id'], 'follow_id' => $data['follow_id']]);

        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
        return $this->db->trans_status() ? true : false;
    }

    public function followList($id)
    {
        $query = $this->db->select('follow_id')
            ->where('user_id', $id)
            ->get('follow');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function getUserInfoByIds($ids)
    {
        $query = $this->db->select('id,nickname,image,sign')
            ->where_in('id', $ids)
            ->get('users');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function fansList($id)
    {
        $query = $this->db->select('user_id')
            ->where('follow_id', $id)
            ->get('follow');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function bindPhoneSubmit($data)
    {
        $this->db->update('users', ['phone' => $data['phone']], ['id' => $data['id']]);
        return $this->db->affected_rows() ? true : false;
    }

    public function isSignIn($data)
    {
        $res = $this->db->select('keep_time,init_date')
            ->where($data)->get('signinlog');
        return $res->num_rows() ? $res->result_array() : false;
    }

    public function signIn($data)
    {
        $this->db->insert('signinlog', $data, ['user_id' => $data['user_id']]);
        return $this->db->affected_rows() ? true : false;
    }

    public function getSignInKeep($uid, $time)
    {
        $query = $this->db->select('keep_time')->where(['user_id' => $uid, 'init_date' => $time])->get('signinlog');
        $keep = 1;
        if (!empty($query->row_array())) {
            $res = $query->row_array();
            $keep += $res['keep_time'];
        }
        return $keep;
    }

    public function existTagName($name)
    {
        $res = $this->db->select('count(id) num')->where('name', $name)->get('tags');
        $num_array = $res->result_array();
        return $num_array[0]['num'] ? true : false;
    }

    public function addTag($data)
    {
        $res = $this->existTagName($data['name']);
        if (!$res) {
            $this->db->insert('tags', $data);
            return $this->db->affected_rows() ? true : false;
        }
        return true;
    }

    public function tagList($id)
    {
        $query = $this->db->select('name')->where('user_id', $id)->or_where('user_id', 0)->get('tags');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function existFavoritesName($data)
    {
        $query = $this->db->select('count(id) num')->where('user_id', $data['user_id'])->where('name', $data['name'])->get('favorites');
        $num_array = $query->result_array();
        return $num_array[0]['num'] ? true : false;
    }

    public function addFavorites($data)
    {
        $this->db->insert('favorites', $data);
        return $this->db->affected_rows() ? true : false;
    }

    public function favoritesList($id)
    {
        $query = $this->db->select('name,id')
            ->where('favorites.user_id', $id)->get('favorites');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function existCollection($data)
    {
        $query = $this->db->select('count(id) num')->where('user_id', $data['user_id'])->where('article_id', $data['article_id'])->get('collection');
        $num_array = $query->result_array();
        return $num_array[0]['num'] ? true : false;
    }

    public function collection($data)
    {
        $this->db->insert('collection', $data);
        return $this->db->affected_rows() ? true : false;
    }

    public function collectionList($id)
    {
        $query = $this->db->select('f_id,article_id,collection.user_id,title')
            ->join('article','article.id=collection.article_id','left')
            ->where('collection.user_id',$id)->get('collection');
        return $query->num_rows() ? $query->result_array() : false;
    }

    public function myRucksack($uid)
    {
        $query = $this->db->select('prid, desc ,prop_name ,num')
            ->from('rucksack c')
            ->join('prop a', 'c.prid = a.id', 'left')
            ->where(['uid' => $uid])
            ->get();
        return $query->num_rows() ? $query->result_array() : '';
    }

    //判断三方登录
    public function checkTrilateralLogin($type, $id)
    {
        return $this->commonQuery('count(id) num', ['type' => $type, 'auth_id' => $id], 'trilateral_login');
    }

    //
    public function TrilateralRegister($nickname, $password, $type, $auth_id)
    {
        $this->db->insert('users', ['nickname' => $nickname, 'password' => $password, 'username' => $nickname, 'create_time' => time()]);
        $user_id = $this->db->insert_id('users');
        $this->commonInsert('trilateral_login', ['type' => $type, 'user_id' => $user_id, 'auth_id' => $auth_id, 'init_time' => time()]);
        return $this->commonQuery('*', ['id' => $user_id], 'users');
    }

    public function TrilateralLogin($auth_id)
    {
        $user_id = $this->commonQuery('user_id', ['auth_id' => $auth_id], 'trilateral_login');
        $user_id = $user_id[0]['user_id'];
        return $this->commonQuery('*', ['id' => $user_id], 'users');
    }

    public function image()
    {
        $this->db->update('users', ['image' => "https://api.multiavatar.com/" . rand(10000, 99999999) * rand(1, 20)]);
        return $this->db->affected_rows() ? 1 : 0;
    }
}